import money from 'src/api/money';
import meEndpoint from 'src/api/meEndpoint';
import { setIsLogin, resetAuth } from 'src/redux/authSlice';
import { resetRebate } from 'src/redux/rebateSlice';
import { dispatch } from 'src/redux/store';
import { finishWaiting, startWaiting } from 'src/redux/uiSlice';
import { setAccountInfo } from 'src/redux/authSlice';
import { init } from 'src/service/appService';
import {
    setRebateStatementKids,
    setRebateStatementMe,
    setRebateStatementMembers,
} from 'src/redux/rebateSlice';
type PostAuthRequest = any


export const getMyRechargeAddres = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await money.getMyRechargeAddres(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const getMyWithdraw = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await money.getMyWithdraw(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const getMyRecharge = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await money.getMyRecharge(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const addMyAddress = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await money.addMyAddress(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const delMyAddress = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await money.delMyAddress(data);
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const getMyAddress = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await money.getMyAddress(data);
        // if (res.data.success) {
        //     const userData = await meEndpoint.postMe(data)
        //     dispatch(setAccountInfo(userData?.data?.list[0]));
        // }
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};

export const addWithdraw = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await money.addWithdraw(data);
        if (res.data.success) {
            init()
        }
        return data.data = res.data
    } finally {
        dispatch(finishWaiting());
    }
};
export const getUserReport = async (data: PostAuthRequest) => {
    try {
        dispatch(startWaiting());
        const res = await money.getUserReport(data);
        if (res.data.success) {
            let data = res.data.list;
            let me = {};
            let arr: any[] = [];

            data.forEach((item: any) => {
                if (item.FMail == "MY") {
                    me = item
                } else {
                    arr.push(item)
                }
            });
            dispatch(setRebateStatementMe(me));
            if (arr.length > 0) {
                dispatch(setRebateStatementMembers(data))
                dispatch(setRebateStatementKids(arr))
            }
        }
    } finally {
        dispatch(finishWaiting());
    }
};